package com.zq.it.dao.impl.exam;

import java.util.List;

import org.hibernate.FetchMode;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.zq.it.dao.exam.TClassTaskDAO;
import com.zq.it.dao.impl.BaseDao;
import com.zq.it.pojo.exam.TClassTask;

/**
 * A data access object (DAO) providing persistence and search support for
 * TClassTask entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see pojo.TClassTask
 * @author MyEclipse Persistence Tools
 */
public class TClassTaskDAOImpl extends BaseDao<TClassTask> implements TClassTaskDAO {

	public static final String DIR = "dir";
	public static final String EXTEND_FIELD = "extendField";
	private static final Logger log = LoggerFactory
			.getLogger(TClassTaskDAOImpl.class);

	@Override
	public void save(TClassTask transientInstance) {
		log.debug("saving TClassTask instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	@Override
	public void delete(TClassTask persistentInstance) {
		log.debug("deleting TClassTask instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	@Override
	public TClassTask findById(java.lang.Long id) {
		log.debug("getting TClassTask instance with id: " + id);
		try {
			TClassTask task = (TClassTask)getHibernateTemplate().findByCriteria(DetachedCriteria.forClass(TClassTask.class)
					.add(Restrictions.idEq(id)).setFetchMode("TClass",FetchMode.JOIN)).get(0);
			return task;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	@Override
	public List findByExample(TClassTask instance) {
		log.debug("finding TClassTask instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	@Override
	public List findByProperty(String propertyName, Object value) {
		log.debug("finding TClassTask instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from TClassTask as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	@Override
	public List findByDir(Object dir) {
		return findByProperty(DIR, dir);
	}

	@Override
	public List findByExtendField(Object extendField) {
		return findByProperty(EXTEND_FIELD, extendField);
	}

	@Override
	public List findAll() {
		log.debug("finding all TClassTask instances");
		try {
			String queryString = "from TClassTask";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	@Override
	public void update(TClassTask instance) {
		log.debug("attaching dirty TClassTask instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	@Override
	public void deleteByHql(String hql) {
		super.deleteEntity(hql);
	}

	@Override
	public List findByHql(String hql) {
		log.debug("finding TClassTask instances by Hql");
		try {
			return getHibernateTemplate().find(hql);
		} catch (RuntimeException re) {
			log.error("find by Hql failed", re);
			throw re;
		}
	}
}